package com.gangan.controller.login;


import com.gangan.controller.common.BaseController;
import com.gangan.model.User;
import com.gangan.service.IAuthService;
import com.gangan.service.IRoleService;
import com.gangan.tools.MessageBean;
import com.gangan.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  登录相关的前端控制层
 *  一切登录、退出登录、忘记密码、注册都将在该控制层进行
 * </p>
 *
 * @author zhengweicheng
 * @since 2019-08-13
 */
@Api(description = "登录相关控制层")
@RestController
@RequestMapping("/login")
public class LoginController extends BaseController {
    @Autowired
    IUserService userService;
    @Autowired
    IAuthService authService;
    @Autowired
    IRoleService roleService;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="loginname",dataType="String",required=true,value="账号",defaultValue="admin"),
            @ApiImplicitParam(paramType="query",name="password",dataType="String",required=true,value="密码",defaultValue="11111111")
    })
    public MessageBean login(@RequestParam("loginname") String username,
                             @RequestParam("password") String password){
        if(StringUtils.isBlank(username)){
            return resultFailed("用户名不可为空");
        }else if(StringUtils.isBlank(password)){
            return resultFailed("密码不可为空");
        }
        //声明存储失败的字符串变量
        String failed;
        Subject subject = SecurityUtils.getSubject();
        try{
            UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username,password);
            usernamePasswordToken.setRememberMe(true);
            //请求shiro登陆认证中。
            subject.login(usernamePasswordToken);
            String token = subject.getSession().getId().toString();
            return new MessageBean(SUC,"登录成功！",null,token,resultUser());
        }catch (UnknownAccountException e) {
            failed = "账号不存在";
        }catch (IncorrectCredentialsException e) {
            failed = "密码错误";
        }catch (LockedAccountException e) {
            failed = "帐号已冻结或未审核";
        }catch (Exception e) {
            //输出异常
            e.printStackTrace();
            return new MessageBean(10010,"登录失败",null);
        }
        return resultFailed(failed);
    }

    /**
     * 退出登录,销毁session
     * @return
     */
    @RequestMapping(value = "logout",method = RequestMethod.GET)
    public MessageBean logout(){
        SecurityUtils.getSubject().logout();
        return new MessageBean(10100,"已成功退出登录",null);
    }
}
